import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui'

// 创建实例
const http = axios.create({
    baseURL:process.env.VUE_APP_BASE_API,
    timeout:5000
})
// 请求拦截
http.interceptors.request.use(config=>{
  // 统一注入token
  if(store.getters.token){
    // token存在 那么就注入token
    config.headers['Authorization']=`Bearer ${store.getters.token}`
  }
  return config //请求配置
},error=>{
  return Promise.reject(error)
})
// 响应拦截
http.interceptors.response.use(response=>{
    // return response
    let {data,message,success} =response.data
  if(success){
    const arr=['/company/department','/sys/user/simple','/sys/profile','/sys/role','sys/user/604f764971f93f3ac8f365c2']
        if (!arr.includes(response.config.url)) {
            Message.success(message)
        }
        return data
  }else{
    Message.error(message)
    return Promise.reject(message)
  } 
},error=>{
   // error信息 里面有response的对象
   if(error.response&&error.response.data&&error.response.status===401){
    // token过期
    Message.warning('token过期了')
    store.display('my_user/logout')
    router.push('/login')
  }else{
    Message.error(error)
  }
  Message.error(error.message)
  return Promise.reject(error)
  
})
 export default http